//-----------------------------------------------------------------
// TextBox methods
//-----------------------------------------------------------------
#include "../Base/stdafx.h"
#include "../Base/GameEngine.h"
#include "../Base/DxDevice.h"
#include "../Sprites/Sprite.h"
#include "../Sprites/SpriteRender.h"
#include "TextBox.h"
#include "../Base/DirectInput.h"

int TextBox::m_Active = 0;
int TextBox::m_Total = 0;

TextBox::TextBox(const tstring& text, const D3DXVECTOR3& pos, int width, int height, const tstring& name, bool isPass) : m_SpriteRenderPtr(0)
{
	++m_Total;
	m_Armed = false;
	m_Current = m_Total;
	m_Text = text;
	m_Name = name;
	m_IsPass = isPass;
	m_SpritePtr = new Sprite(_T("./graphic/index/TextBox.png"), pos, 2);
	m_SpriteRenderPtr = new SpriteRender();
	m_SpriteRenderPtr->AddSprite(m_SpritePtr);

	SetBounds((int)pos.x, (int)pos.y, width, height);
}

TextBox::~TextBox()
{
	if(m_Total == m_Current && m_Current == m_Active)
		--m_Active;
	--m_Total;
	delete m_SpriteRenderPtr;
}

void TextBox::SetBounds(int x, int y, int width, int height)
{
	m_x = x;
	m_y = y;

	m_Width = width;
	m_Height = height;

	float sX = m_Width / m_SpritePtr->GetWidth();
	float sY = m_Height / m_SpritePtr->GetHeight();

	m_SpritePtr->SetPosition(D3DXVECTOR3((float)m_x, (float)m_y, 0));
	m_SpritePtr->SetScale(D3DXVECTOR3(sX, sY, 1));
}

RECT TextBox::GetRect() const
{
	RECT rc;
	
	rc.left = m_x;
	rc.right = m_x + m_Width;
	rc.top = m_y; 
	rc.bottom = m_y + m_Height;

	return rc;
}

void TextBox::SetEnabled(bool bEnable)
{
	if(bEnable)
		m_Active = m_Current;
	else
		m_Active = (m_Active = m_Total)?(1):(m_Active-1);

	m_Enabled = bEnable;
}

const tstring& TextBox::GetText() const
{
	return m_Text;
}

void TextBox::SetText(const tstring& textRef)
{
	m_Text = textRef;
}

void TextBox::Paint() const
{
	m_SpriteRenderPtr->Draw();
	tstring out = _T("");
	tstring cursor = _T("");
	if(m_Enabled && m_Cursor)
		cursor = _T("|");
	if(m_IsPass)
		for(int t = 0; t < (int)m_Text.size(); ++t)	out += _T("*");
	else
		out = m_Text;

	out += cursor;
	DXDEVICE->DrawText(out, m_x + 10, m_y, m_Width, m_Height, DT_LEFT | DT_VCENTER);
}

void TextBox::Tick(float dTime)
{
	POINT pos;
	GetCursorPos(&pos);
	ScreenToClient(GAME_ENGINE->GetWindow(), &pos);

	if(PtInRect(&GetRect(), pos))
	{
		if(DirectInput::GetSingleton()->MouseButtonDown(0))
			m_Armed = true;
		else
		{
			if(m_Armed)
				SetEnabled(true);
			m_Armed = false;
		}
	}

	if(m_Active == m_Current) 
		m_Enabled = true;
	else
		m_Enabled = false;

	if(m_Enabled)
	{
		m_OldTime += dTime;
		if(m_OldTime >= 0.6f)
		{
			m_Cursor = !m_Cursor;
			m_OldTime = 0;
		}

		m_SpritePtr->SetStep(1);
	}else
		m_SpritePtr->SetStep(0);
}

void TextBox::KeyDown(WPARAM key)
{
	if(m_Enabled)
	{
		if(key >= 96 && key <= 105)
			key -= 48;
		if(key >= 48 && key <= 90)
		{
			if(DirectInput::GetSingleton()->KeyDown(DIK_LSHIFT) || key <= 57)
				m_Text += key;
			else
				m_Text += (key+32);
		}
		if(key == VK_BACK)
			m_Text = m_Text.substr(0, m_Text.size()-1);
		if(key == VK_SPACE)
			m_Text += _T(" ");
	}
}

bool TextBox::IsEnabled() const
{
	return m_Enabled;
}

void TextBox::SetPassBox(bool isPass)
{
	m_IsPass = isPass;
}